package com.swust.security.service.impl;

import com.swust.security.entity.User;
import com.swust.security.entity.SecurityUser;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.swust.security.mapper.UserMapper;
import com.swust.security.security.DefaultPasswordEncoder;
import com.swust.security.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("userDetailsService")
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private UserService userService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        System.out.println("登录用户名:"+username);
        //根据用户名查询数据
        User user = userService.selectByUsername(username);
        //判断
        if(user == null) {
            System.out.println("用户不存在");
            throw new UsernameNotFoundException("用户不存在");
        }
        User curUser = new User();
        BeanUtils.copyProperties(user,curUser);
        //根据用户查询用户权限列表
        List<String> permissionValueList = userService.selectAuthByUsername(user.getUsername());
        for(String p:permissionValueList){
            System.out.println(p);
        }
        SecurityUser securityUser = new SecurityUser();
        securityUser.setCurrentUserInfo(curUser);
        securityUser.setPermissionValueList(permissionValueList);
        return securityUser;
    }
}
